package com.github.ecbp.common.data.utils;

import com.github.ecbp.common.api.IErrorCode;
import com.google.common.base.Optional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public enum CommonCodeAndMessageEnum implements IErrorCode {
    //201-299系统模块错误
    SAVE_ERROR(10001, "数据保存失败"),
    UPDATE_ERROR(10002, "数据更新失败"),
    DELETE_ERROR(10003, "数据删除失败"),
    ;

    //code
    private Integer code;

    //描述
    private String message;

    /**
     * 根据code获取状态值信息枚举
     *
     * @param code
     * @return 对应的枚举信息，如果code不存在，则返回null
     * @throws NullPointerException code为空则抛出该异常
     */
    public static CommonCodeAndMessageEnum getByCode(Integer code) {
        Optional.of(code);
        List<CommonCodeAndMessageEnum> codeAndMessageEnums = Arrays.asList(CommonCodeAndMessageEnum.values());
        Map<Integer, CommonCodeAndMessageEnum> messageEnumMapByCode =
                codeAndMessageEnums.stream().collect(Collectors.toMap(CommonCodeAndMessageEnum::getCode, e -> e));
        return messageEnumMapByCode.get(code);
    }

    CommonCodeAndMessageEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}
